﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;


/// <summary>
/// 生成资源配置文件
/// 编译器类:获取完整路径和预设体名
/// 只要知道名字就可以获得预制件路径和名字
/// </summary>
public class GenerateResConfig : Editor
{
    //在菜单栏Tools下增加菜单Resource-Generate ResConfig File
    [MenuItem("Tools/Resource/Generate ResConfig File")]
    public static void Generate()
    {
        string path = "Assets/Resources";//查找文件的路径
        string type = "prefab";//查找文件的后缀名
                               //Debug.Log("ok");
                               //生成映射文件
                               //1.获取Resources文件夹下的所有预制件
        string[] resFiles = AssetDatabase.FindAssets("t:" + type, new string[] { path });
        for (int i = 0; i < resFiles.Length; i++)
        {
            //GUID-->路径
            resFiles[i] = AssetDatabase.GUIDToAssetPath(resFiles[i]);
            //[0] = "Assets/Resources/FX/Player/rune 10.prefab" 转换成 FX/Player/rune 10
            //2.生成对应关系
            //预制件名 完整路径
            string fileName = Path.GetFileNameWithoutExtension(resFiles[i]);
            string filePath = resFiles[i].Replace(path + "/", "").Replace("." + type, "");
            resFiles[i] = fileName + "=" + filePath;
        }
        //3.写入文件，只适合PC端
        File.WriteAllLines("Assets/StreamingAssets/Resconfig.txt", resFiles);//StreamingAssets目录不会压缩，配置文件文本文件放在这里,注意/
        AssetDatabase.Refresh();//刷新
    }
}


